package player

import (
	"GameServer/Common/data"
	"GameServer/Common/utils"
	"GameServer/Game/errors"
	"GameServer/Game/lib"
	"GameServer/Game/sp"
)

// 8501 不扣钱，购买钻石
func (p *Player) ShopDiamondBuyRequest(req *sp.ShopDiamondBuyRequest) (res *sp.ShopDiamondBuyResponse, err error) {
	// 只有超级用户可以免费买钻石
	if p.User.SuperAccount != 1 {
		res = &sp.ShopDiamondBuyResponse{
			Errcode: data.Errcode.ServerErr,
		}
		return
	}

	err = lib.RecordShopBuyItem(p.User, req.Tid)
	if err != nil {
		res = &sp.ShopDiamondBuyResponse{
			Errcode: errors.ErrCode(err),
		}
		return res, err
	}

	uProp, resGoods, boxResList, err := lib.BuyDiamond("ShopDiamondBuyRequest", p.User, req.Tid, p.Send)
	if err != nil {
		res = &sp.ShopDiamondBuyResponse{
			Errcode: errors.ErrCode(err),
		}
		return res, err
	}

	// 首充状态设置
	if p.User.FirstReChargeStatus == 0 {
		p.User.SetFirstRechargeStatus(1)
	}
	res = &sp.ShopDiamondBuyResponse{
		Errcode:             0,
		Res:                 lib.FormatGoodsInfoSps(resGoods),
		BoxRes:              lib.FormatBoxResSps(boxResList),
		FirstReChargeStatus: p.User.FirstReChargeStatus,
	}

	p.TimelyUpdateProp(req.GetType(), uProp)
	utils.AsyncFunc(func() {
		p.UpdateProp(req.GetType(), uProp)
	})
	return
}
